package models;

import play.*;
import play.data.validation.Required;
import play.db.jpa.*;
import play.modules.search.*;

import javax.persistence.*;

import java.util.*;

@Indexed
@Entity
public class Course extends Model {
	
	@Field
	@Required
	public String name;
	
	@Field
	public String professor;
	
	@Field
	public String description;
	
	@Field
	public String room;
	
	@ManyToMany(mappedBy="attendedCourses")
	public Set<User> attendees;
	
	@OneToMany(cascade = CascadeType.ALL, mappedBy = "course")
	public List<Lecture> lectures;
	
	@OneToMany(cascade = CascadeType.ALL, mappedBy = "course")
	public List<NewsItem> news;
	
	public Course(String name, String description, String professor) {
		this.name = name;
		this.professor = professor;
		this.description = description;
		create();
	}
	
	/**
	 * Add a lecture
	 */
	public Lecture newLecture(String name, Date date) {
		Lecture lecture = new Lecture(this, name, date);
		this.refresh();
		return lecture;
	}
	
	/**
	 * Find all lectures for this course
	 */
	
	public List<Lecture> getLectures() {
		return Lecture.find("course = ? order by date asc", this).fetch();
	}
}